﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace eStock.SQL
{
    public class SQLDataReader
    {
        private enum DBType
        {
            Sqlite,
            Mysql
        }

        private readonly object _reader;
        private readonly DBType _type;

        public int FieldCount {get
        {
            switch(_type)
            {
                case DBType.Mysql:
                    return ((MySqlDataReader) _reader).FieldCount;
                case DBType.Sqlite:
                    return ((SQLiteDataReader)_reader).FieldCount;
            }
            return 0;
        }}

        public bool IsValid {get { return _reader != null; }}

        public SQLDataReader(MySqlDataReader reader)
        {
            _reader = reader;
            _type = DBType.Mysql;
        }
        public SQLDataReader(SQLiteDataReader reader)
        {
            _reader = reader;
            _type = DBType.Sqlite;
        }
        
        public uint GetUInt32(string row)
        {
            switch (_type)
            {
                case DBType.Mysql:
                    return ((MySqlDataReader) _reader).GetUInt32(row);
                case DBType.Sqlite:
                    return (uint)((SQLiteDataReader) _reader).GetInt32(((SQLiteDataReader) _reader).GetOrdinal(row));
            }
            return 0;
        }
        public int GetOrdinal(string row)
        {
            switch (_type)
            {
                case DBType.Mysql:
                    return ((MySqlDataReader)_reader).GetOrdinal(row);
                case DBType.Sqlite:
                    return ((SQLiteDataReader)_reader).GetOrdinal(row);
            }
            return 0;
        }
        public DateTime GetDateTime(string row)
        {
            switch (_type)
            {
                case DBType.Mysql:
                    return ((MySqlDataReader)_reader).GetDateTime(row);
                case DBType.Sqlite:
                    return ((SQLiteDataReader)_reader).GetDateTime(((SQLiteDataReader)_reader).GetOrdinal(row));
            }
            return new DateTime();
        }

        public bool Read()
        {
            switch (_type)
            {
                case DBType.Mysql:
                    return ((MySqlDataReader)_reader).Read();
                case DBType.Sqlite:
                    return ((SQLiteDataReader)_reader).Read();
            }
            return false;
        }

        public string GetName(int i)
        {
            switch (_type)
            {
                case DBType.Mysql:
                    return ((MySqlDataReader)_reader).GetName(i);
                case DBType.Sqlite:
                    return ((SQLiteDataReader)_reader).GetName(i);
            }
            return "";
        }

        public object GetValue(int i)
        {
            switch (_type)
            {
                case DBType.Mysql:
                    return ((MySqlDataReader)_reader).GetValue(i);
                case DBType.Sqlite:
                    return ((SQLiteDataReader)_reader).GetValue(i);
            }
            return null;
        }

        public long GetBytes(int i, long dataOffset,byte[] buffer,int bufferOffset, int length)
        {
            switch (_type)
            {
                case DBType.Mysql:
                    return ((MySqlDataReader)_reader).GetBytes(i, dataOffset, buffer, bufferOffset, length);
                case DBType.Sqlite:
                    return ((SQLiteDataReader)_reader).GetBytes(i,dataOffset,buffer,bufferOffset,length);
            }
            return 0;
        }
    }
}
